import { ref, watch } from 'vue';
import { threeListDetailApi, threeListRecordsDetailApi } from '/@/api/threeList/declare';
import { useRoute } from 'vue-router';
import { defaultValue } from '../utils/default';
import { ElLoading } from 'element-plus';
import { getRouterParams } from '/@/utils/hyFunc';
export function useDetail() {
	const route = useRoute();
	const basicInfoData: any = ref(defaultValue().basicInfoDefault);
	//基本清单--已选项目
	const integratedApplicationListData: any = ref([]);
	//需求清单
	const checkListData = ref([]);
	const countryData = ref([]);
	const cityData = ref([]);
	const pepoleData = ref([]);
	const enterpriseData = ref([]);
	const substrateData = ref([]);
	const areaData = ref([]);
	//改革清单
	const reformListData = ref([]);
	//附件清单
	const fileListData = ref({});
	//场景-核心
	const sceneCoreListData = ref([]);
	//场景-一级
	const sceneOneLevelData = ref([]);
	//场景-贯通层
	const sceneLevelFunctionData = ref({});
	//详情-翻译字段
	const basicNameInfoData: any = ref({
		...basicInfoData.value,
		belongDomainName: '',
		deployPortsName: '',
		isWorkTeamName: '',
		constructTypeName: '',
		threeListStatusName: '',
		declarationTime: '',
		belongingRunwayName: '',
		teamLeaderUnitName: '',
	});
	//是否撤销
	const isCancel = ref(false);
	//是否撤回
	const isReturn = ref(false);
	//流程ID
	const processInstanceId = ref('');
	//路由参数
	const routerParams: any = ref({});
	//获取详情
	const getDetail = (threeListId: string) => {
		if (!threeListId) return;
		let params;
		let detailRequest;
		//查询历史进入，用不同的字段和接口
		if (routerParams.value.operateType === 'history') {
			params = { historyVersionId: threeListId };
			detailRequest = threeListRecordsDetailApi;
		} else {
			params = {
				threeListId,
			};
			detailRequest = threeListDetailApi;
		}
		const loading = ElLoading.service({
			lock: true,
			text: '获取数据中...',
		});
		return detailRequest(params)
			.then((res: any) => {
				isCancel.value = res?.isCancel || false;
				isReturn.value = res?.isReturn || false;
				setData(res);
				return res;
			})
			.finally(() => loading.close());
	};
	//处理数据
	const setData = (res: any) => {
		processInstanceId.value = res.processInstanceId;
		mergeInfoData(basicInfoData.value, res);
		mergeInfoData(basicNameInfoData.value, res);
		integratedApplicationListData.value = res?.integratedApplicationList || [];
		checkListData.value = res?.demandList?.demandListTypes || [];
		countryData.value = res?.demandList?.partyDecisionDemandList || [];
		cityData.value = res?.demandList?.cityGovernmentDemandList || [];
		pepoleData.value = res?.demandList?.massesDemandList || [];
		enterpriseData.value = res?.demandList?.enterpriseDemandList || [];
		substrateData.value = res?.demandList?.basicLevelDemandList || [];
		areaData.value = res?.demandList?.reformDemandList || [];
		reformListData.value = res?.reformListList || [];
		fileListData.value = res?.threeListFile || {};
		sceneCoreListData.value = res?.sceneCoreList || [];
		sceneOneLevelData.value = res?.sceneOneLevelList || [];
		sceneLevelFunctionData.value = res?.sceneLevelFunction || {};
		return {
			basicNameInfoData,
			basicInfoData,
			integratedApplicationListData,
			checkListData,
			countryData,
			cityData,
			pepoleData,
			enterpriseData,
			substrateData,
			areaData,
			reformListData,
			fileListData,
			sceneCoreListData,
			sceneOneLevelData,
			sceneLevelFunctionData,
		};
	};
	// 合并两个对象，并处理特殊字段
	const mergeInfoData = (target: any, source: any) => {
		Object.keys(target).forEach((key) => {
			target[key] = source[key];
			if (key === 'constructType') {
				target[key] = source[key] ? source[key].split(',') : [];
			}
		});
	};
	//设置标题
	const title = ref('');
	const setTtile = (type: any) => {
		if (type === 'add') {
			title.value = '添加三张清单';
		}
		if (type === 'edit') {
			title.value = '编辑三张清单';
		}
		if (type === 'declare') {
			title.value = '三张清单申报';
		}
		if (type === 'audit') {
			title.value = '审核三张清单';
		}
		if (type === 'view') {
			title.value = '三张清单详情';
		}
	};

	watch(
		[() => route.query],
		() => {
			routerParams.value = getRouterParams();
			getDetail(routerParams.value?.threeListId);
			setTtile(routerParams.value?.operateType);
		},
		{ immediate: true, deep: true }
	);
	return {
		basicInfoData,
		integratedApplicationListData,
		checkListData,
		countryData,
		cityData,
		pepoleData,
		enterpriseData,
		substrateData,
		areaData,
		title,
		reformListData,
		fileListData,
		sceneCoreListData,
		sceneOneLevelData,
		sceneLevelFunctionData,
		basicNameInfoData,
		getDetail,
		routerParams,
		setData,
		isCancel,
		isReturn,
		processInstanceId,
	};
}
